const express = require('express')

const app = express()

// app.use('/public1', express.static('../dist'))
// 定义一个简单的中间件
// const mw = function (req, res, next) {
//   console.log('第一个中间件')
//   next()
// }
// //将mw注册为一个全局生效的中间件
// app.use(mw)

app.use(function (req, res, next) {
  // console.log('简单的中间件')
  const timer = Date.now()
  req.startTime = timer
  next()
})
app.use(express.urlencoded({ extended: false }))
// 挂在路由
const customerBodyParse = require('./hook/bodyParse')
app.use(customerBodyParse)

const router = require('./router')
app.use('/api', router)

// 错误中间件一定要注册在所有的路由之后
app.use((err, req, res, next) => {
  console.log('Error', +err.message)
  res.send(err.message)
})

app.listen(80, () => {
  console.log('express server running at http://127.0.0.1')
})
